package array

import "sort"

func findRadius(houses []int, heaters []int) int {
	sort.Ints(heaters)

	ans := -1

	for _, house := range houses {
		j := sort.SearchInts(heaters, house)
		var minDis int
		if j == len(heaters) {
			minDis = j - heaters[len(heaters)-1]
		} else if j == 0 {
			minDis = heaters[0] - house
		} else {
			if heaters[j] == house {
				minDis = 0
			} else {
				x := heaters[j] - house
				y := house - heaters[j-1]
				if x < y {
					minDis = x
				} else {
					minDis = y
				}
			}
		}

		if minDis > ans {
			ans = minDis
		}
	}
	return ans
}
